﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace community.Controllers
{
    [Route("api/community-events")]
    [ApiController]
    public class CommunityEventsController : ControllerBase
    {
        private static List<CommunityEvent> events = new List<CommunityEvent>();

        // 发布活动
        [HttpPost]
        public IActionResult AddEvent([FromBody] CommunityEvent newEvent)
        {
            newEvent.Id = events.Count + 1;
            newEvent.CreatedAt = DateTime.UtcNow;
            newEvent.UpdatedAt = DateTime.UtcNow;
            events.Add(newEvent);
            return CreatedAtAction(nameof(GetEventById), new { id = newEvent.Id }, newEvent);
        }

        // 查询所有活动
        [HttpGet]
        public IActionResult GetAllEvents()
        {
            return Ok(events);
        }

        // 根据ID查询活动
        [HttpGet("{id}")]
        public IActionResult GetEventById(int id)
        {
            var ev = events.Find(e => e.Id == id);
            if (ev == null)
            {
                return NotFound();
            }
            return Ok(ev);
        }

        // 更新活动信息
        [HttpPut("{id}")]
        public IActionResult UpdateEvent(int id, [FromBody] CommunityEvent updatedEvent)
        {
            var ev = events.Find(e => e.Id == id);
            if (ev == null)
            {
                return NotFound();
            }

            ev.Title = updatedEvent.Title;
            ev.Description = updatedEvent.Description;
            ev.StartTime = updatedEvent.StartTime;
            ev.EndTime = updatedEvent.EndTime;
            ev.Location = updatedEvent.Location;
            ev.MaxParticipants = updatedEvent.MaxParticipants;
            ev.UpdatedAt = DateTime.UtcNow;

            return NoContent();
        }

        // 删除活动
        [HttpDelete("{id}")]
        public IActionResult DeleteEvent(int id)
        {
            var ev = events.Find(e => e.Id == id);
            if (ev == null)
            {
                return NotFound();
            }

            events.Remove(ev);
            return NoContent();
        }
    }
}
